package com.ymh.spring.learn.mq.activitymq.producer;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

/**
 * 
 * 项目名称：spring-mq 功能描述：
 * 
 * @author aivin
 * @version 1.0.0 ©版权所有 杨明华
 */
public class ProducerServiceImpl implements ProducerService {

	@Autowired
	private JmsTemplate jmsTemplate;
	// @Resource(name = "queueDestination") //队列模式
	@Resource(name = "topicDestination")
	private Destination destination;

	// TODO 生产者发送消息的方法
	public void sendMessage(final String message) {
		jmsTemplate.send(destination, new MessageCreator() {

			public Message createMessage(Session session) throws JMSException {
				TextMessage textMessage = session.createTextMessage(message);
				System.out.println("发送消息" + textMessage.getText());
				return textMessage;
			}
		});
		System.out.println("发送消息" + message);
	}

}
